flyui.namespace("flyui.apps.byexpressionprofile");

/**
 * @class
 * @constructor
 */
flyui.apps.byexpressionprofile.Application = function() {
	
	var that = this;
	
	var flyatlasprofile = null;
	var genefinder = null;
    var flyatlas = null;
    var flyted = null;
    var bdgp = null;
	
	this.initQueryTable = function() {
	    /*
        <table>
            <thead>
                <tr><th>brain</th></tr>
            </thead>
            <tbody>
                <tr>
                    <td><select id="select_brain_change"><option value="any">any</option><option value="up">up</option><option value="down">down</option><option value="nochange">no change</option></select></td>
                </tr>
            </tbody>
        </table>
	     */
	     var content = "<table><thead><tr>";
	     for (var i=1;i<flyui.flyatlas.tissues.length;i++) {
	         var tissue = flyui.flyatlas.tissues[i];
	         content += "<th>"+tissue+"</th>";
	     }
	     content += "</tr></thead><tbody><tr>";
	     
         for (var i=1;i<flyui.flyatlas.tissues.length;i++) {
             var tissue = flyui.flyatlas.tissues[i];
             content += "<td><select id=\"select_"+tissue+"_change\"><option value=\"any\">any</option><option value=\"Up\">up</option><option selected=\"selected\" value=\"Down\">down</option><option value=\"None\">none</option></select></td>";
         }
	     content += "</tr></tbody></table>";
	     $("#queryTableContainer").html(content);
	}
	
	
	this.initFlyatlasProfile = function() {
	    var context = "flyui.apps.byexpressionprofile.Application.initFlyatlasProfile";
	    try {

            flyui.debug("instantiate a service for the flyatlas profile widget", context);
            var service = new flyui.flyatlas.Service();
            service.setEndpoint("../../../data/flyatlas");
            
            flyui.debug("instantiate a renderer for the batchgenefinder widget", context);
            var renderPane = $("#flyatlasProfileWidget");
            var renderer = new flyui.flyatlas.ProfileWidget.DefaultRenderer(); // default renderer
            renderer.setCanvas(renderPane);
            
            flyui.debug("instantiate a flyatlas profile widget", context);
            flyatlasprofile = new flyui.flyatlas.ProfileWidget(service, renderer);
	        
        } catch (error) {
            flyui.apps.byexpressionprofile.handleError(error);
        }
	}
		
    this.initGeneFinder = function() {
        try {
            // initialise genefinder widget
    
            flyui.info("instantiate a service for the genefinder widget");
            var service = new flyui.flybase.Service("../../../data/flybase");
            
            flyui.info("instantiate a renderer for the genefinder widget");
            var renderPane = document.getElementById("geneFinderWidget");
            var renderer = new flyui.genefinder.DefaultRenderer(); // default renderer
            renderer.setCanvas(renderPane);
            
            flyui.info("instantiate a genefinder widget");
            genefinder = new flyui.genefinder.Widget(service, renderer);
            
        } catch (error) {
            flyui.apps.byexpressionprofile.handleError(error);
        }
    };
        
    // called by this.init
    
    this.initFlyatlas = function() {
        try {
            
            flyui.info("instantiate a service for the flyatlas widget");
            var service = new flyui.flyatlas.Service();
            service.setEndpoint("../../../data/flyatlas");
            
            flyui.info("instantiate a renderer for the flyatlas widget");
            var renderer = new flyui.flyatlas.Widget.DefaultRenderer(); 
            renderer.setCanvas($("#flyatlasWidget"));
            
            flyui.info("instantiate a flyatlas widget");
            flyatlas = new flyui.flyatlas.Widget(service, renderer);
            
        } catch (error) {
            flyui.apps.expressionmashup.handleError(error);
        }
    };
    
    // called by this.init
    
    this.initFlyted = function() {
        try {
            
            flyui.info("instantiate a service for the flyted image widget");
            var service = new flyui.flyted.Service("../../../data/flyted");
            
            flyui.info("instantiate a renderer for the flyted image widget");
            var renderPane = document.getElementById("flytedImageWidget");
//            var renderer = new CustomFlytedRenderer();
            var renderer = new flyui.flyted.ImageWidget.DefaultRenderer();
            renderer.setCanvas(renderPane);
            
            flyui.info("instantiate a flyted image widget");
            flyted = new flyui.flyted.ImageWidget(service, renderer);
            
        } catch (error) {
            flyui.apps.expressionmashup.handleError(error);
        }
    };
    
    // called by this.init
    
    this.initBdgp = function() {
        try {
            
            flyui.info("instantiate a service for the bdgp image widget");
            var service = new flyui.bdgp.Service("../../../data/bdgp");
            
            flyui.info("instantiate a renderer for the bdgp image widget");
            var renderPane = document.getElementById("bdgpImageWidget");
//            var renderer = new CustomBdgpRenderer();
            var renderer = new flyui.bdgp.ImageWidget.DefaultRenderer();
            renderer.setCanvas(renderPane);
            
            flyui.info("instantiate a bdgp image widget");
            bdgp = new flyui.bdgp.ImageWidget(service, renderer);
            
        } catch (error) {
            flyui.apps.expressionmashup.handleError(error);
        }
    };
    
    this.init = function() {
        var context = "flyui.apps.byexpressionprofile.Application.init";
        try {
    		
    		var logReader = new YAHOO.widget.LogReader("logger");

            this.initQueryTable();
            this.initFlyatlasProfile();
            this.initGeneFinder();
            this.initFlyatlas();
            this.initFlyted();
            this.initBdgp();

            flyui.debug("hook form submission to widget call", context);
            YAHOO.util.Event.addListener("queryForm", "submit", this.onFormSubmitted);
            
            flyui.debug("add listener for results received event", context);
            flyatlasprofile.subscribe("RESULTSRECEIVED", this.onResultsReceived, null);
            
            // attach gene selection listener to genefinder
            genefinder.subscribe("GENESELECTED", this.onGeneSelected, null);

    		flyui.debug("application loaded, showing main pane", context);
    		$("#loadingPane").hide();
    		$("#applicationPane").show();
    		
        } catch (error) {
            flyui.apps.byexpressionprofile.handleError(error);
        }
	};
	
	this.onFormSubmitted = function(event) {
        var context = "flyui.apps.byexpressionprofile.Application.onFormSubmitted";
        try {
            flyui.debug("form submitted", context);
            $("#errorMessage")
               .hide()
               .html("this should never be visible");
            $("#geneFinderWidget").hide();
            $("#flyatlasWidget").hide();
            $("#flytedImageWidget").hide();
            $("#bdgpImageWidget").hide();

            var profile = {};
            
            flyui.debug("build profile object", context);
            for (var i=1;i<flyui.flyatlas.tissues.length;i++) {
                var tissue = flyui.flyatlas.tissues[i];
                var selectid = "select_"+tissue+"_change";
                var select = document.getElementById(selectid);
                var value = select.value;
                flyui.debug("found value "+value+" for select "+selectid, context);
                profile[tissue] = value;
            }
            
            flyui.debug("initiate request for probes and genes by profile", context);
            flyatlasprofile.getProbesAndGenesByExpressionProfile(profile);
            
        } catch (error) {
            flyui.apps.byexpressionprofile.handleError(error);
        }
	    
	};

    this.onResultsReceived = function(type, args) {
        try {
            flyui.debug("application: results received");
            var results = args[0];
            var flybaseids = results.genes;
            
            genefinder.findGenesByAnyNameBatch(flybaseids);
            $("#geneFinderWidget").show();

        } catch (error) {
            flyui.apps.byexpressionprofile.handleError(error);
        }
    };
    
    this.onGeneSelected = function(type, args) {
        try {
            var gene = args[0];
            flyui.info("gene selected: "+gene+" "+gene.flybaseID);
            flyatlas.getAssaysByGene(gene);
            flyted.findImagesByGene(gene);
            bdgp.findImagesByGene(gene);
            $("#flyatlasWidget").show();
            $("#flytedImageWidget").show();
            $("#bdgpImageWidget").show();
//            that.setSelectionState(gene);
        } catch (error) {
            flyui.apps.expressionmashup.handleError(error);
        }
    };  
    	
	this.init();
};

flyui.apps.byexpressionprofile.handleError = function(error) {
    flyui.err(error.name+" :: "+error.message);
    $("#errorMessage")
       .html("an error has occurred, please see the log for more details")
       .show();
};
    

// on dom ready, instantiate application
YAHOO.util.Event.onDOMReady(function() {new flyui.apps.byexpressionprofile.Application();});